package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${cfg.parent}.common.controller.BaseController;
import ${package.Entity}.${entity};
import ${cfg.parent}.common.model.BaseModel;
import ${cfg.parent}.common.model.BaseResp;
import ${package.Service}.I${entity}Service;
import ${cfg.parent}.common.utils.ResponseUtils;
import ${cfg.parent}.common.utils.IdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


@Api(value = "${table.comment}Controller", tags = { "${table.comment}控制器" })
@RestController
@RequestMapping("/${cfg.modelName}/${table.entityPath}")
@Slf4j
public class ${entity}Controller extends BaseController {

	@Autowired
	private I${entity}Service ${table.entityPath}ServiceImpl;

	/**
	* 添加记录
	*/
	@ApiOperation(value = "添加记录")
	@RequestMapping(value = "add")
	public Object add(@ApiParam(name = "entity", value = "添加的对象entity")@ModelAttribute("entity") ${entity} entity) throws Exception {
		BaseResp resp = new BaseResp();
<#assign columns = table.fields />
<#list columns as column>
<#assign chnName=column.comment/>
<#assign fieldName=column.propertyName/>
<#assign fieldType=column.propertyType/>
<#assign columnName=column.name/>
<#assign capitalName=column.capitalName/>
<#assign primary=column.keyFlag/>
	<#if primary = true>
		entity.set${capitalName}(IdUtils.id());
	</#if>
</#list>
		boolean result= ${table.entityPath}ServiceImpl.save(entity);
		resp.setData(result);
		return resp;
	}

	/**
	* 修改记录
	*/
	@ApiOperation(value = "修改记录")
	@RequestMapping(value = "modify")
	public Object modify(@ApiParam(name = "entity", value = "修改的对象entity")@ModelAttribute("entity") ${entity} entity) throws Exception {
		BaseResp resp = new BaseResp();
		boolean result= ${table.entityPath}ServiceImpl.updateById(entity);
		resp.setData(result);
		return resp;
	}

	/**
	* 根据主键指删除记录
	*/
	@ApiOperation(value = "根据主键指删除记录")
	@RequestMapping(value = "delete")
	public Object delete() throws Exception {
		BaseResp resp = new BaseResp();
		String ids = request.getParameter("ids");
		boolean result = ${table.entityPath}ServiceImpl.removeByIds(Arrays.asList(ids.split(",")));
		resp.setData(result);
		return resp;
	}

	/**
	* 根据主键获取记录信息
	*/
	@ApiOperation(value = "根据主键获取记录信息")
	@RequestMapping(value = "queryById/{id}")
	public Object queryById(@ApiParam(name = "id", value = "记录的ID")@PathVariable("id")Long id) throws Exception {
		BaseResp resp = new BaseResp();
		${entity} record = ${table.entityPath}ServiceImpl.getById(id);
		resp.setData(record);
		return resp;
	}

	/**
	* 查询记录信息
	*/
	@ApiOperation(value = "查询记录信息")
	@RequestMapping(value = "query")
	public Object query(@ApiParam(name = "entity", value = "查询记录条件")@ModelAttribute("entity") ${entity} entity) throws Exception {
		if (entity == null) {
			entity = new ${entity}();
		}
		Page page = new Page(entity.getPage(),entity.getLimit());
		QueryWrapper<${entity}> ew = entity.toWrapper();
		page = ${table.entityPath}ServiceImpl.page(page,ew);
		return ResponseUtils.pagePlusResp(page);
	}

	/**
	* 获取所有记录信息，不分页
	*/
	@ApiOperation(value = "获取所有记录信息，不分页")
	@RequestMapping(value = "queryAll")
	public Object queryAll(@ApiParam(name = "entity", value = "查询记录条件") @ModelAttribute("entity") ${entity} entity) throws Exception {
		BaseResp resp = new BaseResp();
		if (entity == null) {
			entity = new ${entity}();
		}
		Page page = new Page(entity.getPage(), BaseModel.maxLimit);
		QueryWrapper<${entity}> ew = entity.toWrapper();
		page = ${table.entityPath}ServiceImpl.page(page,ew);
		resp.setData(page.getRecords());
		return resp;
	}

}